//
//  PopoverAnimator.swift
//  Weibo
//
//  Created by 孙国强 on 2017/6/21.
//  Copyright © 2017年 孙国强. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    var isPresented : Bool = false
    var presentedViewFrame : CGRect = CGRect.zero
    
    var callBack : ((_ presented : Bool) -> ())?
    
    // MARK:- 自定义构造函数
    init(callBack : @escaping (_ presented : Bool) -> ()) {
        self.callBack = callBack
    }
}

// MARK:- 自定义转场代理方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    // 目的: 设置要弹出的控制器的样式
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        // UIPresentationController的init方法
        let presentationController : SGQPresentationController = SGQPresentationController(presentedViewController: presented, presenting: presenting)
        presentationController.presentedViewFrame = presentedViewFrame
        return presentationController
    }
    
    // 目的: 自定义弹出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        callBack!(isPresented)
        return self
    }
    
    // 目的: 自定义消失的动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBack!(isPresented)
        return self
    }
}

// MARK:- 弹出和消失动画的代理方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning{
    // 动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View
    // UITransitionContextFromViewKey : 获取消失的View
    // UITransitionContextToViewKey : 获取弹出的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissedView(transitionContext: transitionContext)
    }
    
    // 自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        // 获取弹出的View
        let presentedView = transitionContext.view(forKey: .to)!
        
        // 将弹出的View添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        
        // 执行动画
        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.identity
        }) { (_) in
            // 必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    
    // 自定义消失动画
    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {
        // 获取消失的View
        let dismissView = transitionContext.view(forKey: .from)!
        
        // 执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView.transform = CGAffineTransform(scaleX: 1.0, y: 0.000001)
        }) { (_) in
            dismissView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}
